<?php
class controller_checkout extends __registry_controller
{
    public $_ = array();
    public $_layout = NULL;
    public $type = array('bank','paypal','money');
    public $limit = 2;
    
    public $_content_header = 'OPEN';
    public $_content_top = 'OPEN';
    
	public function __construct()
    {	
		$this->_layout =NULL;
        parent::add_object(array(
            'model_checkout',
            'handle_paging',
            'handle_upload',
            'handle_images',
            'model_permission'
        ));
	}
	
	public function index_action($cid = null)
	{
	   
        /* Check permission */
        $this->per_check_permission();

       $seo2 = isset($this->_seokey[2])?$this->_seokey[2]:null;
       $this->_['com'] = $this->_seokey[1];

       /*****************pagination**********************/
       $limit = $this->limit;
       $current = end($this->_seokey)?intval(end($this->_seokey)):1;
       $total = $this->mdcheckout_get_total('check_out');
       $url = !empty($cid)?URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/'.$seo2:URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1];
       $this->_['paging'] = $this->paging_display($total, $current, $limit, $url, $this->_,'/','.htm');
       
       $this->_['url_add'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('add');
       $this->_['url_edit'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('edit');
	   
       $strHtmlContent = '';
       $strOkHtml = '';
       $html = $this->view->view_display("index");
       $arrHtml = $this->view->view_get_block_html($html);

       $list_item = $this->mdcheckout_get_list_item($current,$limit);
       if(!empty($list_item)){
            foreach($list_item as $item){
                $strOkHtml .= $this->view->view_assign($arrHtml['category'],$item);
            
            }
       }
       $strHtmlContent = $this->view->view_assign_block($html, 
                                                        array(
                                                            'htmlCat'=>$strOkHtml
                                                        )
       );
	   return $strHtmlContent;
	}
    
    public function add_action(){
        /* Check permission */
        $this->per_check_permission();
        
        $this->_['label-error'] = '';
        $this->_layout = 'iframe';
        $this->_['com'] = 'check_out';
        $this->_['error'] = '';
        if(isset($_POST) && !empty($_POST)){
            $name = isset($_POST['name']) && $_POST['name'] != ''?$_POST['name']:'';

            if($name != '')
            {
                $info = isset($_POST['info']) && $_POST['info'] != ''?$_POST['info']:'';
                $type = isset($_POST['type']) && $_POST['type'] != ''?$_POST['type']:'';
                $status = $_POST['Status'];
                $config_p = $_POST['config'];
                if(isset($config_p['title']) && !empty($config_p['title']))
                {
                    $config = array();
                    foreach($config_p['title'] as $key=>$title)
                    {
                        if($title != '')
                        {
                            $config[$title] = $config_p['content'][$key];
                        }
                    }
                }
                $config = json_encode($config);
                
                $data = array();
                $data['Name'] = $name;
                $data['Type'] = $type;
                $data['Info'] = $info;
                $data['Status'] = $status;
                $data['Config'] = $config;
                $data['Post_Date'] = date('Y-m-d');
                $data['Edit_date'] = date('Y-m-d');
                
                $result = $this->mdcheckout_save_data('check_out', $data);
                $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/');     
            }else
            {
                $this->_['error'] = $this->_['error_label'];   
            }
            
        }
        /******************************list type**********************/
        $strHtml = $this->view->view_display('add');
        $arrHtml = $this->view->view_get_block_html($strHtml);
        $strOkHtml = '';
        foreach($this->type as $type)
        {
            $strOkHtml .= $this->view->view_assign($arrHtml['type'],
                                                    array(
                                                            'type'           => $type,
                                                            'selected_type'       => ''
                                                    )
            );
        }
        
        $strHtmlContent = $this->view->view_assign_block($strHtml, array('list_type'=>$strOkHtml));
        return $strHtmlContent;
    }
    
    public function edit_action(){
        /* Check permission */
        $this->per_check_permission();
        
        $this->_['label-error'] = '';
        $this->_layout = 'iframe';
        $this->_['com'] = 'check_out';
        $this->_['error'] = '';
        $id = $this->_seokey[3];
        if(isset($_POST) && !empty($_POST)){
            $name = isset($_POST['name']) && $_POST['name'] != ''?$_POST['name']:'';

            if($name != '')
            {
                $info = isset($_POST['info']) && $_POST['info'] != ''?$_POST['info']:'';
                $type = isset($_POST['type']) && $_POST['type'] != ''?$_POST['type']:'';
                $status = $_POST['Status'];
                $config_p = $_POST['config'];
                if(isset($config_p['title']) && !empty($config_p['title']))
                {
                    $config = array();
                    foreach($config_p['title'] as $key=>$title)
                    {
                        if($title != '')
                        {
                            $config[$title] = $config_p['content'][$key];
                        }
                    }
                }
                $config = json_encode($config);
                
                $data = array();
                $data['Name'] = $name;
                $data['Type'] = $type;
                $data['Info'] = $info;
                $data['Status'] = $status;
                $data['Config'] = $config;
                $data['Edit_date'] = date('Y-m-d');
                
                $result = $this->mdcheckout_update_data('check_out', $data, 'ID = '.$id);
                $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/');     
            }else
            {
                $this->_['error'] = $this->_['error_label'];   
            }
            
        }
        /******************************list type**********************/
        $strHtml = $this->view->view_display('edit');
        $arrHtml = $this->view->view_get_block_html($strHtml);
        $strOkHtml = '';
        $strOkHtmlTabTitle = '';
        $strOkHtmlTabContent = '';
        $item = $this->mdcheckout_get_item('check_out', $id);
        if(empty($item))
        {
            $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/');    
        }
        
        /**********list type**********************/
        foreach($this->type as $type)
        {
            $select_type = '';
            if($item['Type'] == $type)
            {
                $select_type = 'selected=""';
            }
            $strOkHtml .= $this->view->view_assign($arrHtml['type'],
                                                    array(
                                                            'type'           => $type,
                                                            'selected_type'       => $select_type
                                                    )
            );
        }
        /*********list tab*************************/
        $tab_list = $item['Config'];
        $this->_['total_tab'] = 1;
        $error_tab = 0;
        if(!empty($tab_list))
        {
            $tab_list = json_decode($tab_list);
            if(!empty($tab_list))
            {
                $i = 0;
                $this->_['total_tab'] = count($tab_list);
                foreach($tab_list as $key=>$tab)
                {
                    $strOkHtmlTabTitle .= $this->view->view_assign($arrHtml['tab_title'],
                                                              array(
                                                                      'id_tab'=>$i,
                                                                      'tab_title'=>$key     
                                                              )
                    );
                    $strOkHtmlTabContent .= $this->view->view_assign($arrHtml['tab_content'],
                                                              array(
                                                                      'id_tab'=>$i,
                                                                      'tab_content'=>$tab
                                                              )
                    );
                    $i++;
                }
            }
        }
        
        /**********list status*********************/
        $arr_status = array('true'=>'true','false'=>'false');
        $strOkHtmlStatus = '';
        foreach($arr_status as $key=>$status)
        {
            $select_status = '';
            if($item['Status'] == $status)
            {
                $select_status = 'selected=""';
            }
            $strOkHtmlStatus .= '<option '.$select_status.' value="'.$key.'">'.$this->_[$status].'</option>';
        }
        $this->_['html_status'] = $strOkHtmlStatus;
        
        $strHtmlContent = $this->view->view_assign_block($strHtml, 
                                                        array(
                                                            'list_type'=>$strOkHtml,
                                                            'list_tab_title'=>$strOkHtmlTabTitle,
                                                            'list_tab_content'=>$strOkHtmlTabContent,
                                                        )
        );
        $strHtmlContent = $this->view->view_assign($strHtmlContent, $item);
        return $strHtmlContent;
    }
     
    public function index_ajax_action(){
        /* Check permission */
        $this->per_check_permission();
        
        $this->_layout = 'NO_LAYOUT';
        $action = isset($this->_seokey[3])?$this->_seokey[3]:null;
        $str_id = isset($this->_seokey[4])?$this->_seokey[4]:null;
        $str_path = isset($this->_seokey[5])?$this->_seokey[5]:null;
        $html = '';
        if(isset($action)){
            switch($action)
            {
               case "hide":
                   $done = $this->mdcheckout_tool_func($this->model,$action,$str_id);
                    if($done){
                        return 'ok';
                    }else{
                        return 'noOk';
                    }
                   break;
               case "show":
                   $done = $this->mdcheckout_tool_func($this->model,$action,$str_id);
                    if($done){
                        return 'ok';
                    }else{
                        return 'noOk';
                    }
                   break;
               case "delete":
                   $done = $this->mdcheckout_tool_func($this->model,$action,$str_id);
                    if($done){
                        return 'ok';
                    }else{
                        return 'noOk';
                    }
                   break;
               default:
            }
            return $html;    
        }
    }
    
	
}